package com.yonyou.pmclouds.async;

import com.yonyou.pmclouds.basecom.async.AsyncInfoTransactionSynchronization;
import com.yonyou.pmclouds.basecom.async.AsyncInvocationInfo;
import com.yonyou.pmclouds.basecom.async.TransactionAsyncInvocationManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.support.TransactionSynchronizationManager;

@Service
public class DefaultTransactionAsyncInvocationManager implements TransactionAsyncInvocationManager {
    @Autowired
    private AsyncTaskPusher pusher;
    @Autowired
    private AsyncInvocationInfoMaintain maintain;

    @Override
    public AsyncInvocationInfo makeInfo() {
        return pusher.makeInfo();
    }

    @Override
    public AsyncInvocationInfo prepare(AsyncInvocationInfo info) {
        AsyncInvocationInfo preparedInfo = pusher.prepare(info);
        maintain.insertInfo(info);
        // 向spring事务管理器注册事务，事务提交后，可提交该任务
        TransactionSynchronizationManager.registerSynchronization(new AsyncInfoTransactionSynchronization(pusher, info));
        return preparedInfo;
    }
}
